package com.xwb.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.xwb.domain.Member;
import com.xwb.domain.MemberWithTutor;
import com.xwb.domain.SupplyForUser;
import com.xwb.domain.User;
import org.apache.ibatis.annotations.*;

import java.util.List;
//member实体类：
//private String competitionId; // 比赛号码
//private String teamNumber; // 队伍号
//private String memberPosition; // 职务，队长/队员
//private String userId; // 学号 (Primary Key)
//private String memberPhoneNumber; // 手机号
//private String memberCollege; // 学院
//private String memberClass; // 班级
//private String memberName; // 名字

@Mapper
public interface MemberDao extends BaseMapper<Member> {

    //成员表（包括队长、队员）的报名信息保存
    @Insert("insert into member " +
            "values(#{competitionId},#{teamNumber},#{memberPosition},#{userId}," +
            "#{memberPhoneNumber},#{memberCollege},#{memberClass},#{memberName}) ")
     void saveInfo(Member member);



//        //查询同队成员和导师信息的信息
//    @Select("SELECT m.competition_id, m.team_number,  m.member_name,m.member_position, m.user_id, m.member_phoneNumber,\n" +
//            "       m.member_college, m.member_class,\n" +
//            "       t.tutor_name, t.tutor_college, t.tutor_phoneNumber,\n" +
//            "CASE WHEN member_position = '队长' THEN 1 ELSE 2 END AS position_order\n" +
//            "FROM member m\n" +
//            "JOIN tutor t ON t.team_number = m.team_number\n" +
//            "ORDER BY m.team_number,position_order;")
//     List<MemberWithTutor> selectSameTeamInfo(String teamNumber);

//================================================================================================

    /**
     * 保存队员信息
     */
    @Insert("insert into team (competition_id, team_number, member_name, member_position, user_id, " +
            "member_phoneNumber, member_college, member_class ) " +
            "values(#{competitionId},#{teamNumber},#{memberName},#{memberPosition},#{userId}," +
            "#{memberPhoneNumber},#{memberCollege},#{memberClass}) ")
    void saveMemberInfo(Member member);

    /**
     * 保存邀请用户信息到team表
     */
    @Insert("insert into team(competition_id, team_number,member_name, " +
            "user_id, member_phoneNumber, member_college, member_class) " +
            "VALUES(#{competitionId},#{teamNumber},#{userName},#{userId},\" +\n" +
            "            \"#{memberPhoneNumber},#{memberCollege},#{memberClass}) ")
    void saveUserInfo(User user);

    /**
     * 保存队长和导师信息
     */
    @Insert("insert into team (competition_id, team_number, " +
            "member_name, team_name," +
            "member_position, user_id, member_phoneNumber, " +
            "member_college, member_class, tutor_name, " +
            "tutor_college, " +
            "tutor_phoneNumber) values" +
            "(#{competitionId},#{teamNumber}," +
            "#{memberName},#{teamName}," +
            "#{memberPosition},#{userId},#{memberPhoneNumber}," +
            "#{memberCollege},#{memberClass}," +
            "#{tutorName},#{tutorCollege},#{tutorPhoneNumber})")
    void saveLeaderAndTutorInfo(MemberWithTutor memberWithTutor);

    /**
     * 根据队伍号查询出同队的成员信息
     */
    @Select("select * from team where team_number = #{teamNumber}")
   List<MemberWithTutor> selectByTeamNumber(String teamNumber);

    /**
     * 根据team表中的编号（serial_number）删除学生信息
     */
    @Delete("delete from team where serial_number = #{SerialNumber}")
    void deleteBySerialNumber(Integer SerialNumber);

//    /**
//     *将邀请的人的信息从user表里查询出来然后插入到team表中
//     */
//    @Insert("INSERT INTO team (member_name,user_id, member_phoneNumber, member_college, member_class)\n" +
//            "SELECT user_name,  user_id, user_phoneNumber, user_college, user_class\n" +
//            "FROM user\n" +
//            "WHERE user_id = #{receiverId};\n")
//    void insertInvitationReceiverToTeam(String receiverId);
//
//    /**
//     * 将邀请的人的比赛号码，队伍号，队伍名等更新一致
//     */
//    @Update("UPDATE team\n" +
//            "SET competition_id = (SELECT competition_id FROM team WHERE user_id = #{receiverId} AND member_position = '队员'),\n" +
//            "    team_number = (SELECT team_number FROM team WHERE user_id = #{receiverId} AND member_position = '队员'),\n" +
//            "    team_name = (SELECT team_name FROM team WHERE user_id = #{receiverId} AND member_position = '队员'),\n" +
//            "    member_position = '队员'\n" +
//            "WHERE user_id = #{receiverId};\n")
//    void updateInvitationReceiverMessage(String receiverId);

    /**
     * 补充邀请成员的信息
     */
    @Update("update team set member_position = '队员'," +
            "team_number = #{teamNumber},team_name = #{teamName} where user_id = #{userId}")
    void supplyUserInfo(SupplyForUser supplyForUser);

}
